Insert current date or time
There are a variety of ways to automatically add a date/time stamp to your editing buffer, similar to in Windows Notepad. Using strftime() Vim's internal strftime() function ( ) returns a date/time string, given a format string, but this might not work on all systems. To store return value of this Vim function for pasting, the "= register ( ) is used. Here's a bunch of examples: This makes ' in insert mode' paste a datestamp for an effect like that in Notepad. ( ): :inoremap =strftime("%c") This makes dts typed in insert mode expand to a datestamp ( ): :iab dts =strftime("%c") This will cause ' in normal mode' to paste the current datestamp. :nnoremap "=strftime("%c")P In the example above, the capital P at the end inserts before the current character, which allows datestamps inserted at the beginning of an existing line. Other 'put' commands may be more useful for you: To embed the current date in a substitution: :s/text to replace with date/\=strftime("%c")/ Vary the format string, i.e. the "%c" argument to strftime(), to get the time and/or date in a different format. Depending on the format string used, the result may depend on your locale ( ). The specification for the format string itself depends on the implementation of strftime() on your platform. For details, Unix users may refer to the strftime(3) man page, by running 'man 3 strftime' at the prompt. Some strftime() format string examples Format String Example output ------------- -------------- %c Thu 27 Sep 2007 07:37:42 AM EDT (depends on locale) %a %d %b %Y Thu 27 Sep 2007 %b %d, %Y Sep 27, 2007 %d/%m/%y %H:%M:%S 27/09/07 07:36:32 %H:%M:%S 07:36:44 %T 07:38:09 %m/%d/%y 09/27/07 %y%m%d 070927 %x %X (%Z) 09/27/2007 08:00:59 AM (EDT) RFC822 format: %a, %d %b %Y %H:%M:%S %z Wed, 29 Aug 2007 02:37:15 -0400 ISO8601/W3C format (http://www.w3.org/TR/NOTE-datetime): %FT%T%z 2007-08-29T02:37:13-0400 Using external tools Unix users have access to the date utility. You may ask Vim to run 'date' and insert the output in the buffer: :r!date Under Windows, :r!date /t Alternatively, Windows users may install cygwin from http://sources.redhat.com/cygwin for access to Unix 'date' and a host of other utilities, but that might be overkill if you're just looking for datestamps. Automatically update timestamps You might want to automatically update existing time stamps when writing a file. This is a solution for html implemented as an autocmd which fires when the file is written: :au BufWritePre *.html exe "norm mz"|exe '%s/\(\).\{-}\d\d:\d\d:\d\d/\1'.strftime("%b %d, %Y %X")."/e"|norm `z That way a string of the form Aug 13, 2001 14:19:50 is embedded in the text, and it will be updated to the current date (using Vim's built-in strftime() function) and time automatically – every time I save the file (the ...DATE... stuff is an HTML comment which won't appear in an HTML document). This is a general solution: " Add a function that returns a time stamp in the desired format. if !exists("*TimeStamp") fun TimeStamp() return "Time-stamp: <" . strftime("%d %b %Y %X") . " My Name>" endfun endif " Search for an existing time stamp and update it. if !exists("*UpdateTimeStamp") fun UpdateTimeStamp() if (match(getline(1),"Time-stamp: <.*>")) > 1 exe "1,1 s/Time-stamp: <.*>/" . TimeStamp() endif endfun endif " Abbreviation to manually enter a timestamp (type YTS in insert mode). iab YTS =TimeStamp() " Add an autocommand to update an existing time stamp when writing the file. " It uses the functions above to replace the time stamp and restores cursor " position afterwards. autocmd BufWritePre,FileWritePre * ks|call UpdateTimeStamp()|'s The date is searched for in the first line of the file only (you can change that) in columns > 1. I did that mainly for security reasons (in source code, there will be almost always a comment start, e.g. /* in c, preceding the time stamp). Comments You could also use the following :execute commands: nmap tt :execute "normal i" . strftime("%x %X (%Z) ") imap tt :execute "normal i" . strftime("%x %X (%Z) ")i By default, is "\". ( .) In my case I hit "\tt" in normal or insert mode and hello time and date. ---- Category:Automated Text Insertion